package com.ansion.preference;

import android.content.SharedPreferences;
import android.text.TextUtils;

import com.tencent.mmkv.MMKV;


public class MMKVPreference implements ITurboBasePreference {

    private MMKV mKv;

    public MMKVPreference(String key, String cryptKey) {
        if (TextUtils.isEmpty(cryptKey)) {
            mKv = MMKV.mmkvWithID(key, MMKV.SINGLE_PROCESS_MODE);
        } else {
            mKv = MMKV.mmkvWithID(key, MMKV.SINGLE_PROCESS_MODE, cryptKey);
        }
    }

    @Override
    public boolean putFloat(String key, float value) {
        return mKv.encode(key, value);
    }

    @Override
    public float getFloat(String key, float defValue) {
        return mKv.decodeFloat(key, defValue);
    }

    @Override
    public boolean putInt(String key, int value) {
        return mKv.encode(key, value);
    }

    @Override
    public int getInt(String key, int defValue) {
        return mKv.decodeInt(key, defValue);
    }

    @Override
    public boolean putLong(String key, long value) {
        return mKv.encode(key, value);
    }

    @Override
    public long getLong(String key, long defValue) {
        return mKv.decodeLong(key, defValue);
    }

    @Override
    public boolean putString(String key, String value) {
        return mKv.encode(key, value);
    }

    @Override
    public String getString(String key, String defValue) {
        return mKv.decodeString(key, defValue);
    }

    @Override
    public boolean putBoolean(String key, boolean value) {
        return mKv.encode(key, value);
    }

    @Override
    public boolean getBoolean(String key, boolean defValue) {
        return mKv.decodeBool(key, defValue);
    }

    @Override
    public boolean contains(String key) {
        return mKv.contains(key);
    }

    @Override
    public void remove(String key) {
        mKv.remove(key);
    }

    @Override
    public String[] getAllKeys() {
        return mKv.allKeys();
    }

    @Override
    public SharedPreferences.Editor getEditor() {
        return mKv.edit();
    }

    @Override
    public void clear() {
        mKv.clearAll();
    }
}
